package cn.wolfcode.controller;

import cn.wolfcode.domain.Destination;
import cn.wolfcode.query.DestinationQuery;
import cn.wolfcode.repository.DestinationRepository;
import cn.wolfcode.service.IDestinationService;
import cn.wolfcode.service.IStrategyCatalogService;
import cn.wolfcode.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


@Controller
@RequestMapping("destination")
public class DestinationController {
    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    @RequestMapping("list")
    public Object list(Model model,@ModelAttribute("qo") DestinationQuery qo){
        Page query = destinationService.query(qo);
        List<Destination> destinations = destinationService.queryToasts(qo.getParentId());
        model.addAttribute("page",query);
        model.addAttribute("toasts",destinations);
        return "/destination/list";
    }

    @RequestMapping("updateInfo")
    @ResponseBody
    public Object updateInfo(String info,String id){
        if (StringUtils.hasLength(id)){
            Destination destination = destinationService.get(id);
            destination.setInfo(info);
            destinationService.update(destination);
        }
        return JsonResult.success();
    }

    @RequestMapping("delete")
    @ResponseBody
    public Object delete(String id){
        if (id != null){
            destinationService.delete(id);
            return JsonResult.success();
        }
        return JsonResult.defaultError();
    }

}
